import os

#proj_path = "/home/saung/workspace/go-project/gosandbox/"
#proj_path = "/home/ziang/Desktop/cal/gosandbox/gosrc/"
proj_path = "/home/ziang/Desktop/cal/gosandbox/"
ref_path = "/home/ziang/Desktop/cal/gosandbox/gosrc/gatherer/data"
data_path = proj_path + "gatherer/data" #this is a directory storing the data .txt files
hist_path = proj_path + "gatherer/histograms" #this is a directory storing the histograms
#R_path = proj_path + "gatherer/hist.R"
py_path = proj_path + "gatherer/hist.py"

cmd = "find " + proj_path + "parsed_sources/ -name '*.parsed'"
#cmd = "find /home/saung/workspace/go-project/gosandbox/parsed_source/ -name '*.parsed'"
parsedfiles = os.popen(cmd).read().splitlines()

stats = {}

stats['Number of arguments per function'] = []
stats['Number of results per function'] = []
stats['Number of statements per function'] = []
stats['Number of cases per expression switch'] = []
stats['Number of cases per type switch'] = []
stats['Number of cases per select'] = []

numfiles = len(parsedfiles)

print "Parsing parsed files..."

for parsedfile in parsedfiles:
	filestats = {}
	
	f = open(parsedfile, "r")
	lines = f.read().splitlines()
	f.close()
	
	for line in lines:
                if line.startswith('/'):
			continue
		if line.find(':') != -1 and not line.startswith('Function declaration:'):
			splitline = line.split(':')
			key = splitline[0]
                        try:
			  val = float(splitline[1])
                        except:
                          print parsedfile + ', ' + key + ', ' +str(val)
                elif line.startswith('Function declaration:'):
                	key = line.split(':')[0]
                	val = 1.0
		else:
			key = line
			val = 1.0
		if key not in filestats:
			filestats[key] = val
		else:
			filestats[key] += val 

	for key in filestats:
		if key not in stats:
			stats[key] = [filestats[key]]
		else:
			stats[key].append(filestats[key])

	if 'Function type' in filestats:
		stats['Number of arguments per function'].append(filestats['Number of function parameters']/filestats['Function type'])
		stats['Number of results per function'].append(filestats['Number of function results']/filestats['Function type'])
	if 'Number of statements in function body' in filestats:
		stats['Number of statements per function'].append(filestats['Number of statements in function body']/filestats['Function type'])
	if 'Case clause of expression switch statement' in filestats:
		stats['Number of cases per expression switch'].append(filestats['Case clause of expression switch statement']/filestats['Switch statement'])
	if 'Case clause of type switch statement' in filestats:
		stats['Number of cases per type switch'].append(filestats['Case clause of type switch statement']/filestats['Type switch statement'])
	if 'Case clause of select statement' in filestats:
		stats['Number of cases per select'].append(filestats['Case clause of select statement']/filestats['Select statement'])
		

#for key in stats:
#  print key + ':\t\t\t\t' + str(float(sum(stats[key]))/numfiles)
#average_imports = float(sum(stats['Import declaration']))/numfiles
#print average_imports

#NOTE: remember that if a file has no entry for a key, must enter it as 0 in the global stats (should be shown as 0 in the histogram for that file). 

print "Creating data files..."

for key in stats:
	name = key.replace(" ", "_")
	path = os.path.join(data_path, name + ".txt")
	out = open(path, "w")
	#out.write("key\n")
	for val in stats[key]:
		out.write(str(val) + "\n")
        if ' per ' not in key:
		numwrote = len(stats[key])
		while numwrote < numfiles:
			out.write("0\n")
			numwrote += 1
	out.close()

print "Creating histograms..."

cmd = "find " + data_path + " -name '*.txt'"
datafiles = os.popen(cmd).read().splitlines()
for datafile in datafiles:
        pass
	cmd = " ".join(["python", py_path, datafile, hist_path, ref_path])
	os.popen(cmd) 
'''
	fname = datafile.split("/")[-1]
	outname = os.path.join(hist_path, fname.replace(".txt",""))
	cmd = "Rscript " + R_path + " " + datafile + " " + outname + " " + fname.replace(".txt","")
	os.popen(cmd)
'''

print "All done!"

